package cn.e4j.modular.crm.controller;

import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.LangUtils;
import cn.e4j.modular.crm.entity.GwfbT;
import cn.e4j.modular.crm.service.GwfbTService;
import cn.e4j.modular.utils.CrmExceptionEnum;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 年度费用预算维护作业
 * Created by ganjia on 2019-10-25.
 */
@Controller
@RequestMapping("/crm/gwft100")
public class Gwft100Controller extends BaseController {

    @Autowired
    private GwfbTService gwfbTService;

    /**
     * 跳转年度费用预算维护作业页面
     */
    @RequiresPermissions("crm:gwft100:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/crm/gwft100.html";
    }

    /**
     * 跳转到查看编辑的页面
     */
    @RequestMapping("/gwft100_add")
    public String addView() {
        return "/pc/crm/gwft100_edit.html";
    }

    /**
     * 查询销售渠道信息列表
     */
    @RequestMapping("/getGwfblist")
    @RequiresPermissions("crm:gwft100:init")
    @ResponseBody
    public ResponseData getGwfblist(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        ShiroUser shiroUser = ShiroKit.getUser();
        Dto inDto = dtoWapper.getDto();
        inDto.put("ent",shiroUser.getDBC());
        inDto.put("lang",LangUtils.getLang(shiroUser.getLANG()));
        List<Map<String, Object>> result = gwfbTService.getGwfblist(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 新增
     */
    @RequestMapping("/add")
    @RequiresPermissions("crm:gwft100:init")
    @ResponseBody
    public ResponseData add(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        // 判断编号是否重复
        GwfbT theGwfbT = gwfbTService.getByGwfbkey(shiroUser.getDBC(),inDto.getString("GWFBNO"),inDto.getBigDecimal("GWFBSEQ"));
        if (theGwfbT != null) {
            //throw new ServiceException(CrmExceptionEnum.EXISTED_THE_CODE);
            return new ErrorResponseData(CrmExceptionEnum.EXISTED_THE_CODE.getCode() ,
                    CrmExceptionEnum.EXISTED_THE_CODE.getMessage());
        }
        gwfbTService.add(inDto);
        return SUCCESS_TIP;
    }

    /**
     * 修改
     */
    @RequestMapping("/edit")
    @RequiresPermissions("crm:gwft100:init")
    @ResponseBody
    public ResponseData edit(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        gwfbTService.edit(inDto);
        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping("/del")
    @RequiresPermissions("crm:gwft100:init")
    @ResponseBody
    public ResponseData del(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        gwfbTService.delete(inDto);
        return SUCCESS_TIP;
    }

    /**
     * 批量删除数据
     */
    @RequestMapping("/delDataBatch")
    @RequiresPermissions("crm:gwft100:init")
    @ResponseBody
    public ResponseData deleteDataBatch(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwfbT> gwfbTList = inDto.getReqList(GwfbT.class);
        gwfbTService.delDataBatch(gwfbTList);
        return SUCCESS_TIP;
    }

    /**
     * 年度费用预算信息列表
     */
    @RequestMapping("/getlistPage")
    @ResponseBody
    public ResponseData getlistPage(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = gwfbTService.getlistPage(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 批量审核数据
     */
    @RequestMapping("/check")
    @RequiresPermissions("crm:gwft100:check")
    @ResponseBody
    public ResponseData check(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwfbT> gwfbTList = inDto.getReqList(GwfbT.class);
        gwfbTService.check(gwfbTList);
        return SUCCESS_TIP;
    }

    /**
     * 批量取消审核数据
     */
    @RequestMapping("/unCheck")
    @RequiresPermissions("crm:gwft100:unCheck")
    @ResponseBody
    public ResponseData unCheck(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwfbT> gwfbTList = inDto.getReqList(GwfbT.class);
        gwfbTService.unCheck(gwfbTList);
        return SUCCESS_TIP;
    }

    /**
     * 获取汇率
     */
    @RequestMapping("/getHuiLv")
    @ResponseBody
    public ResponseData getHuiLv(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto result = gwfbTService.getHuiLv(inDto);
        return ResponseData.success(result);
    }
}
